#!/sw/bin/perl

use IO::Socket;

my $log_path = "/ZhE/home/ZhE/documents/programming/perl/irc_client/logs";

my $host = 'irc.freenode.net';
my $port = 6667;
my $nick = 'test_client2';
my $ident = 'testclient';
my $realname = 'Mimisbrunnr TestClient';
my @channels = ( '#msqirc' );


my $sock = new IO::Socket::INET ( 
                                 PeerAddr => $host,
                                 PeerPort => $port,
                                 Proto => 'tcp'
                                ) or die "Could not create socket";

print $sock "NICK $nick \r\n";
print $sock ":$nick USER $ident $host bla :$realname\r\n";

for( @channels ) {
  print $sock "JOIN $_\r\n";
}  

print $sock ":$nick!~$ident\@yourmom/ PRIVMSG Mimisbrunnr :alkjsdfhgklaf\r\n";


while ( <$sock> ) {
  print stdout $_;
  for $line (split( /\n\r/, $_ )) {
    $line =~ s/^\s*(\S*(?:\s+\S+)*)\s*$/$1/g;
    if( $line =~ m/:?PING(.+)/ ) {
      print $sock ":$nick!~$ident".'@yourmom PRIVMSG #msqirc :hello'."\r\n";
      print $sock "PONG $1\r\n";
      print stdout ":$nick!~$ident".'@yourmom PRIVMSG #msqirc :hello'."\n";
      print stdout "PONG$1\n";
    }
  }
}

